package j20251009;

public class Demo4 {
    private static class MyRunnable implements Runnable {
        @Override
        public void run() {
            //while(!Thread.isInterrupted())也可以，效果相同
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println(Thread.currentThread().getName()
                        + ": 别管我，我忙着转账呢!");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println(Thread.currentThread().getName()
                            + ": 有内鬼，停止交易！");
                    // 重新设置中断状态
                    Thread.currentThread().interrupt();
                }
            }
            System.out.println(Thread.currentThread().getName()
                    + ": 啊！险些误了⼤事");
        }
    }

    public static void main(String[] args) {
        MyRunnable t=new MyRunnable();
        Thread thread = new Thread(t, "李四");
        System.out.println(Thread.currentThread().getName()
                + ": 让李四开始转账。");
        thread.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()
                + ": 是骗子！让李四转账结束。");
        thread.interrupt();
    }
}
